//
//  GQHRegionController.m
//  Xanthium
//
//  Created by GuanQinghao on 27/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHProvinceModel.h"
#import "GQHCityModel.h"
#import "GQHDistrictModel.h"

#pragma mark Object

#pragma mark View
#import "GQHRegionView.h"

#pragma mark Controller
#import "GQHRegionController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHRegionController () <GQHRegionViewDelegate> {
    
#pragma mark Data
    __block NSMutableArray *provinceModelArray;
    __block NSMutableArray *cityModelArray;
    __block NSMutableArray *districtModelArray;
    
    NSString *selectedID;
    NSString *selectedName;
    NSString *selectedProvinceName;
    NSString *selectedCityName;
    NSString *selectedDistrictName;
    
#pragma mark View
    /// 自定义根视图
    GQHRegionView *rootView;
}

@property (nonatomic, strong) CLLocation * location;

@end


@implementation GQHRegionController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHRegionView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchProvinces];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"选择省市县区";
    [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
-(void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (tableView.tag) {
            
        case 10: {
            
            [rootView.qh_cityNameArray removeAllObjects];
            [rootView.qh_districtNameArray removeAllObjects];
            
            GQHProvinceModel *model = provinceModelArray[indexPath.row];
            selectedID = model.qh_provinceID;
            selectedProvinceName = model.qh_provinceName;
            rootView.qh_resultLabel.text = model.qh_provinceName;
            self.location = [[CLLocation alloc] initWithLatitude:model.qh_provinceLatitude.doubleValue longitude:model.qh_provinceLongitude.doubleValue];
            
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            [parameters setValue:model.qh_provinceID forKey:@"provinceid"];
            
            [self fetchCitiesWithParameters:parameters];
        }
            break;
        case 11: {
            
            [rootView.qh_districtNameArray removeAllObjects];
            
            GQHCityModel *model = cityModelArray[indexPath.row];
            selectedID = model.qh_cityID;
            selectedCityName = model.qh_cityName;
            rootView.qh_resultLabel.text = [selectedProvinceName stringByAppendingString:selectedCityName];
            self.location = [[CLLocation alloc] initWithLatitude:model.qh_cityLatitude.doubleValue longitude:model.qh_cityLongitude.doubleValue];
            
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            [parameters setValue:model.qh_cityID forKey:@"cityid"];
            
            [self fetchDistrictsWithParameters:parameters];
        }
            break;
        case 12: {
            
            GQHDistrictModel *model = districtModelArray[indexPath.row];
            selectedID = model.qh_districtID;
            selectedDistrictName = model.qh_districtName;
            rootView.qh_resultLabel.text = [[selectedProvinceName stringByAppendingString:selectedCityName] stringByAppendingString:selectedDistrictName];
            self.location = [[CLLocation alloc] initWithLatitude:model.qh_districtLatitude.doubleValue longitude:model.qh_districtLongitude.doubleValue];
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    selectedName = rootView.qh_resultLabel.text;
    self.qh_regionBlock(selectedID, selectedName, self.location);
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --PrivateMethod

// 获取省列表数据
- (void)fetchProvinces {
    
    [GQHProvinceModel qh_fetchProvincesWithParameters:nil success:^(BOOL status, NSNumber *code, NSString *message, NSArray *provinces, NSArray *provinceNames) {
        
        if (status) {
            
            provinceModelArray = [NSMutableArray arrayWithArray:provinces];
            rootView.qh_provinceNameArray = (NSMutableArray *)provinceNames;
            [rootView.qh_provincesTableView reloadData];
            [rootView.qh_citiesTableView reloadData];
            [rootView.qh_districtsTableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

// 获取市列表数据
- (void)fetchCitiesWithParameters:(NSDictionary *)parameters {
    
    [GQHCityModel qh_fetchCitiesWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray *cities, NSArray *cityNames) {
        
        if (status) {
            
            cityModelArray = [NSMutableArray arrayWithArray:cities];
            rootView.qh_cityNameArray = (NSMutableArray *)cityNames;
            [rootView.qh_citiesTableView reloadData];
            [rootView.qh_districtsTableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

// 获取县列表数据
- (void)fetchDistrictsWithParameters:(NSDictionary *)parameters {
    
    [GQHDistrictModel qh_fetchDistrictsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray *districts, NSArray *districtNames) {
        
        if (status) {
            
            districtModelArray = [NSMutableArray arrayWithArray:districts];
            rootView.qh_districtNameArray = (NSMutableArray *)districtNames;
            [rootView.qh_districtsTableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
